//
// Created by hugan on 2020/11/16.
//

#include "CommonUtil.h"

bool CommonUtil::toCBool(jboolean v) {
    /*
     * JNI与C++的数据类型转换
     * https://blog.csdn.net/u014300915/article/details/52916086
     */
    return v == JNI_TRUE;
}

void CommonUtil::printRes(const char *desc, jboolean res) {
    if (res == JNI_TRUE) {
        cout << desc << ":success" << endl;
    } else {
        cout << desc << ":failure" << endl;
    }
}

int CommonUtil::activeKeys(vector<WORD> keys) {
    int size = keys.size();
    INPUT inputs[size * 2];
    int i = 0;
    //说明:https://docs.microsoft.com/en-us/windows/win32/api/winuser/ns-winuser-keybdinput
    for (auto key:keys) {
        inputs[i].type = INPUT_KEYBOARD;
        inputs[i].ki = {key, 0, 0, 0, 0};//按下按键
        inputs[size + i].type = INPUT_KEYBOARD;
        inputs[size + i].ki = {key, 0, KEYEVENTF_KEYUP, 0, 0};//抬起按键
        i++;
    }
//    cout << "size=" << size << endl;
//    for (int j = 0; j < size * 2; ++j) {
//        cout << "i=" << j << ", key=" << inputs[j].ki.wVk << endl;
//    }
    return SendInput(size * 2, inputs, sizeof(INPUT));
}
